﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebPartControls_CampusDynamicListControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
        var ds = from news in dc.News
                 join news_to_catalog in dc.NewsToCatalogs
                 on news.News_id equals news_to_catalog.News_id
                 where news_to_catalog.Catalog_id.Equals(5) && news.isPublished.Equals(1)
                 orderby news.Time descending
                 select new
                 {
                     标识 = news.News_id,
                     标题 = news.Title,
                     日期 = news.Time.Value.Month + "-" + news.Time.Value.Day
                 };
        NewsListGridView.DataSource = ds.Take(5);
        NewsListGridView.DataBind();

        int i = 0;
        foreach (var news in ds)
        {
            string News_Title;
            News_Title = Convert.ToString(news.标题);
            News_Title=HtmlFormat.HtmlFormat.DeleteHtmlFormat(News_Title);
            ((LinkButton)NewsListGridView.Rows[i].FindControl("News_Title")).Text = SubString(News_Title, 16);
            if (++i >= 5) break;
        }
        dc.Dispose();
    }
    public string SubString(string sString, int nLength)
    {
        if (sString.Length <= nLength)
        {
            return sString;
        }
        string sNewStr = sString.Substring(0, nLength);
        sNewStr = sNewStr + "...";
        return sNewStr;
    }
    protected void NewsListGridView_OnSelectedIndexChange(object sender, GridViewSelectEventArgs e)
    {
        Session.Add("News_id", ((Label)NewsListGridView.Rows[e.NewSelectedIndex].FindControl("News_id")).Text);
        Response.Redirect("CampusDynamic.aspx");
    }
}
